package com.sikaryofficial.backend.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sikaryofficial.backend.constant.MyStatisticsTypeEnum;
import com.sikaryofficial.backend.domain.entity.UserFeedback;
import com.sikaryofficial.backend.mapper.UserFeedbackMapper;
import com.sikaryofficial.backend.service.IUserFeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 用户反馈Service业务层处理
 *
 * @author qinjinyuan
 * @date 2023-11-16
 */
@Service
public class UserFeedbackServiceImpl extends ServiceImpl<UserFeedbackMapper, UserFeedback> implements IUserFeedbackService {
    @Autowired
    private UserFeedbackMapper feedbackMapper;
    @Resource(name = "asyncTaskExecutor")
    private ThreadPoolTaskExecutor executor;

    /**
     * 删除用户数据
     *
     * @param userId 用户ID
     * @return 结果
     */
    @Override
    public int deleteUserDataByUserId(Long userId) {
        // 使用java自带的CompletableFuture 进行多线程并行执行下面的逻辑
        // 第一个任务
        CompletableFuture<Void> future1 = CompletableFuture.runAsync(() -> feedbackMapper.deleteDataByUserId(userId), executor);
        // 第十三个任务
        CompletableFuture<Void> future2 = CompletableFuture.runAsync(() -> feedbackMapper.deleteMyStatisticsByUserId(userId, buildParam()), executor);
        CompletableFuture.allOf(future1, future2).join();
        return 1;
    }

    private List<Integer> buildParam() {
        return Arrays.asList(
                MyStatisticsTypeEnum.COMMENTS_EVALUATE_NUM.getBusinessType(),
                MyStatisticsTypeEnum.COMMENTS_REPLAY_NUM.getBusinessType(),
                MyStatisticsTypeEnum.RESERVATION_NUM.getBusinessType(),
                MyStatisticsTypeEnum.RESERVATION_EXPIRE_NUM.getBusinessType(),
                MyStatisticsTypeEnum.COLLECTION_PRODUCT_NUM.getBusinessType(),
                MyStatisticsTypeEnum.COLLECTION_STORE_NUM.getBusinessType(),
                MyStatisticsTypeEnum.COLLECTION_ACTIVITY_NUM.getBusinessType(),
                MyStatisticsTypeEnum.FORWARD_STORE_NUM.getBusinessType(),
                MyStatisticsTypeEnum.FORWARD_PRODUCT_NUM.getBusinessType(),
                MyStatisticsTypeEnum.LIKES_PRODUCT_NUM.getBusinessType(),
                MyStatisticsTypeEnum.LIKES_PRODUCT_EVALUATE_NUM.getBusinessType(),
                MyStatisticsTypeEnum.REPORT_NUM.getBusinessType(),
                MyStatisticsTypeEnum.REPORT_ACCEPTED_NUM.getBusinessType(),
                MyStatisticsTypeEnum.WORK_ORDER_ACCEPTING.getBusinessType(),
                // 新增文章及相关内容
                MyStatisticsTypeEnum.LIKES_ARTICLE.getBusinessType(),
                MyStatisticsTypeEnum.LIKES_ARTICLE_EVALUATE.getBusinessType(),
                MyStatisticsTypeEnum.LIKES_ARTICLE_EVALUATE_REPLY.getBusinessType(),
                MyStatisticsTypeEnum.COLLECTION_ARTICLE.getBusinessType(),
                MyStatisticsTypeEnum.FORWARD_ARTICLE.getBusinessType(),
                MyStatisticsTypeEnum.COMMENTS_ARTICLE.getBusinessType(),
                MyStatisticsTypeEnum.COMMENTS_ARTICLE_REPLAY.getBusinessType(),
                MyStatisticsTypeEnum.COMMENTS_ARTICLE_COUNT.getBusinessType(),
                MyStatisticsTypeEnum.COMMENTS_ARTICLE_REPLAY_COUNT.getBusinessType(),
                MyStatisticsTypeEnum.ARTICLE_DYNAMIC_NUM.getBusinessType(),
                MyStatisticsTypeEnum.ARTICLE_LONG_NUM.getBusinessType()
        );
    }
}
